Convolutional Neural Networks and Classification

Classification

  • Regression:
    • output is a prediction on a quantity which takes continuous values
  • Classification:
    • output is a prediction on a class, i.e. discrete labels
      • binary classification
      • multi-class classification
        • single-label classification
        • multi-label classification

Convolutional Neural Network (CNN, ConvNet)

  • a class of artificial Neural Networks
  • widely used in computer vision*
  • learn hierarchy of features

*Computer vision is an essential, complex, wide-spread, and ever developing part of AI. Computer vision tasks include:

  • Image Classification
  • Classification with localization
  • Object detection (verify the presence of specific objects in an image)
  • Semantic segmentation
  • Instance segmentation
  • Neural Style Transfer

Why CNNs?

Image of 1300 X 1300px --> Input on a layer of a FC (Dense) network with e.g. 1000 hidden units -->

parameters (weights) to calculate : (1000,1300x1300) = 1.69E9 parameters (x3 for a color image)

\textbf{Create some simple 3x3 filters and apply with a convolution operator on images }

Convolution Operation

Example with edges filters

In [29]:
filter_v=np.array([[1,0,-1],[1,0,-1],[1,0,-1]])  # 3x3 vertical edges filter 
filter_h=np.array([[1,1,1],[0,0,0],[-1,-1,-1]])  # 3x3 horizontal edges filter 
filter_v_3d=np.dstack([filter_v]*3)                 # 3x3x3 vertical edges filter 
filter_h_3d=np.dstack([filter_h]*3)                 # 3x3x3 horizontal edges filter 

Apply edge detector on gray scale image (nh x nw)

In [30]:
image = Image.open('brick2.jpeg').convert('L')
brick = asarray(image)
plt.figure(figsize=(10,10))
plt.imshow(brick,cmap='gray')
plt.axis('off')
plt.show()
In [31]:
out_conv_h=signal.convolve2d(brick,filter_h,mode='valid')
out_conv_v=signal.convolve2d(brick,filter_v,mode='valid')
f, axarr = plt.subplots(1,2,figsize = (50,50))
axarr[0].imshow(np.absolute(out_conv_h),cmap='gray')
axarr[0].set_axis_off()
axarr[1].imshow(np.absolute(out_conv_v),cmap='gray')
axarr[1].set_axis_off()

plt.show()
In [21]:
image = Image.open('brick.jpeg')
brick = asarray(image)
plt.figure(figsize=(10,10))
plt.imshow(brick)
plt.axis('off')
plt.show()
In [27]:
out_corr_v=signal.correlate(brick,filter_v_3d,mode='valid')
out_corr_h=signal.correlate(brick,filter_h_3d,mode='valid')

f, axarr = plt.subplots(1,2,figsize = (50,50))
axarr[0].imshow(np.absolute(out_corr_h),cmap='seismic')
axarr[0].set_axis_off()
axarr[1].imshow(np.absolute(out_corr_v),cmap='seismic')
axarr[1].set_axis_off()
plt.show()
In [3]:
import tabletext
data = [[1,0,-1],[1,0,-1],[1,0,-1]]
print(tabletext.to_text(data))
┌───┬───┬────┐
│ 1 │ 0 │ -1 │
├───┼───┼────┤
│ 1 │ 0 │ -1 │
├───┼───┼────┤
│ 1 │ 0 │ -1 │
└───┴───┴────┘
In [4]:
data = [[1,1,1],[0,0,0],[-1,-1,-1]]
print(tabletext.to_text(data))
┌────┬────┬────┐
│  1 │  1 │  1 │
├────┼────┼────┤
│  0 │  0 │  0 │
├────┼────┼────┤
│ -1 │ -1 │ -1 │
└────┴────┴────┘

Padding

Strided Convolutions

Convolutions on volumes (RGB images)

Schematic Description of a Convolutional Layer

Layers in ConvNets

The building blocks (i.e. most commonly used types of layers in CNNs)

  • Convolutional layers
  • Pooling layers
    • Max Pooling
    • Average Pooling
    • Global Pooling
  • Dense (Fully Connected) layers

Schematic Description of a ConvNet

CNNs in Tensorflow

In [ ]: